﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Net;
using System.Threading;

namespace tcp_SocketServer_10
{
    class Program
    {
        private static Socket tcp_SocketServer;

        public static List<Client> clientList = new List<Client>();

        public static void BoMessage(string str)
        {
            List<Client> NoConnectClientList = new List<Client>();
            foreach (var client in clientList)
            {
                if (client.IsConnected)
                {
//                     byte[] data = new byte[1024];
//                     client.clientSocket.Send(data);
                    client.SendMessage(str);
                }
                else
                {
                    NoConnectClientList.Add(client);
                }
            }

            foreach(var temp in NoConnectClientList)
            {
                clientList.Remove(temp);
            }

        }

        static void Main(string[] args)
        {
            tcp_SocketServer = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);

            tcp_SocketServer.Bind(new IPEndPoint(IPAddress.Parse("0.0.0.0"), 6688));

            tcp_SocketServer.Listen(100);

            Console.WriteLine("监听完毕");

            while (true)
            {
                Socket tcp_SocketClient = tcp_SocketServer.Accept();

                Console.WriteLine("a client is connect!!!");

                Client client = new Client(tcp_SocketClient);

//                 if (!clientList.Contains(client))
//                 {
                    clientList.Add(client);
/*                }*/

            }


          /* / *Thread td = * / new Thread(ReciverMessage).Start();  */       


        }

        static void ReciverMessage()
        {
            while(true)
            {
               Socket tcp_SocketClient = tcp_SocketServer.Accept();

               Client client = new Client(tcp_SocketClient);

               if (!clientList.Contains(client))
               {
                   clientList.Add(client);
               }

            }
        }
    }
}
